package cn.tedu._04mvcweibo.demos;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Tomcat {
    public static void main(String[] args) throws IOException {
        //1.创建一个服务,并且在指定端口进行监听
        int port=9999;
        ServerSocket server=new ServerSocket(port);
        System.out.println("tomcat start ...");
        //2.等待客户端的连接,并处理客户端的请求,然后返回响应
        while(true) {
            Socket accept = server.accept();
            new Thread(()->{
                try {
                    OutputStream out = accept.getOutputStream();
                    //假设客户端是浏览器,我们们现在要向浏览器响应一个数据
                    String responseData = "HTTP/1.1 200 ok \n\r" +
                            "Content-Type:text/html;charset=utf-8 \n\r" +
                            "\n\r" +
                            "hello client";
                    out.write(responseData.getBytes());
                    out.close();
                }catch (Exception e){e.printStackTrace();}
            }).start();
        }
    }
}
